<template>
  <div class="app-container">
    <el-form
      ref="form"
      :inline="true"
      :model="formData"
      class="demo-form-inline"
    >
      <el-form-item label="商品分类">
        <el-select v-model="formData.typeId" clearable placeholder="商品分类">
          <el-option
            :label="item.typeName"
            :value="item.id"
            v-for="item in categoryList"
            :key="item.id"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="销售平台">
        <el-select v-model="formData.sellType" clearable placeholder="销售平台"    @change="handleChange" >
          <el-option
            v-for="item in productFormTypeList"
            :label="item.label"
            :value="item.value"
            :key="item.value"
          ></el-option>
        </el-select>
      </el-form-item>

        <el-form-item label="销售品牌">
          <el-select
          clearable
          v-model="formData.brandId"
          placeholder="请选择品牌"
        >
          <el-option
            v-for="item in brandList"
            :label="item.brandName"
            :value="item.id"
            :key="item.id"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSearch">查询</el-button>
        <el-button type="primary" @click="reset">重置</el-button>
      </el-form-item>
    </el-form>

    <el-button class="create" type="primary" @click="create">创建</el-button>

    <el-table :data="list" style="width: 100%">
      <el-table-column prop="id" label="ID" width="180"> </el-table-column>
      <el-table-column
        prop="appSkuId"
        label="appSkuId"
        width="180"
      ></el-table-column>
      <el-table-column prop="name" label="商品名称" width="180">
      </el-table-column>
      <el-table-column label="商品分类" width="180"
        ><template slot-scope="scope">
          {{ categoryMap[scope.row.typeId] }}
        </template>
      </el-table-column>

      <el-table-column label="销售平台" width="180">
        <template slot-scope="scope">
          {{ productFormType[scope.row.sellType] }}
        </template>
      </el-table-column>

      <el-table-column prop="price" label="商品价格" width="150">
        <template slot-scope="scope">
          {{ Number(scope.row.price).toFixed(2) }}（元）
        </template>
      </el-table-column>
      <el-table-column prop="washType" label="清洗类型"> </el-table-column>
       <el-table-column prop="brandName" label="品牌名称"> </el-table-column>
      <el-table-column prop="shoesNum" label="鞋子数量"> </el-table-column>
      <el-table-column prop="status" label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status == 1" type="success">上架</el-tag>
          <el-tag v-if="scope.row.status == 2" type="danger">下架</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="remark" label="备注"> </el-table-column>
      <el-table-column prop="updateBy" label="操作人"> </el-table-column>
      <el-table-column prop="updateTime" label="更新时间" width="180">
      </el-table-column>
      <el-table-column label="操作" width="180" fixed="right">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="edit(scope.row.id)"
            >编辑</el-button
          >
          <el-button
            type="text"
            size="small"
            v-if="scope.row.status == 2"
            @click="updateStatus(scope.row, '1')"
            >上架</el-button
          >
          <el-button
            type="text"
            size="small"
            v-if="scope.row.status == 1"
            @click="updateStatus(scope.row, '2')"
            >下架</el-button
          >

          <el-popconfirm
            style="margin-left: 10px"
            title="您确定要删除当前商品吗？"
            @confirm="deleteItem(scope.row.id)"
          >
            <el-button type="text" size="small" slot="reference"
              >删除</el-button
            >
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="queryParams.total > 0"
      :total="queryParams.total"
      :page.sync="queryParams.page"
      :limit.sync="queryParams.pageSize"
      @pagination="getListData"
    />
  </div>
</template>

<script>
import { getBrandByChannel } from '@/api/brand/index'
import {
  getShopGoods,
  addOrEditShopGood,
  deleteShopGood,
  getProductCategories,
} from "@/api/product/index";
import { productFormType, productFormTypeList , productFormTypeListDic} from "@/config/index";
export default {
  name: "Index",
  data() {
    return {
      brandList:[],
      productFormTypeListDic,
      queryParams: {
        page: 0,
        limit: 10,
        total: 0,
        pageSize:10,
      },
      formData: {
        sellType: "",
        typeId: "",
      },
      categoryList: [],
      productFormType,
      productFormTypeList,
      list: [],
      categoryMap: {},
    };
  },
  created() {
    this.getListData({
      page: 1,
      limit: 10,
    });
    this.getProductCategories();
  },
  methods: {

    handleChange(){
     this.brandList=[]
     let brandObj={... this.formData}
     brandObj.brandId=""
     this.formData=brandObj
     let channel=this.productFormTypeListDic[this.formData.sellType];
     if(channel){
        getBrandByChannel(channel).then(res=>{
          this.brandList=res.data
          // this.formData.brandId
        })
     }
    },
    getProductCategories() {
      getProductCategories({}, 1, 9999).then((res) => {
        this.categoryList = res.data.records;
        this.categoryMap = res.data.records.reduce((acc, cur) => {
          acc[cur.id] = cur.typeName;
          return acc;
        }, {});
      });
    },
    getListData({ page, limit }) {
      getShopGoods(
        this.queryParams,
        this.queryParams.page,
        limit
      ).then(({ data }) => {
        this.list = data.records;
        this.queryParams.total = data.total;
        this.queryParams.page = data.current;
        this.queryParams.limit = data.size;
      });
    },
    updateStatus(item, status) {
      addOrEditShopGood({
        ...item,
        status,
      }).then((res) => {
        this.loading = false;
        this.$message.success("操作成功");
        this.getListData(this.queryParams);
      });
    },
    deleteItem(id) {
      deleteShopGood(id).then((res) => {
        this.$message.success("删除成功");
        this.getListData(this.queryParams);
      });
    },
    create() {
      this.$router.push({ path: "/product/edit" });
    },
    edit(id) {
      // console.log(id, "zxcasd");
      this.$router.push({ path: `/product/edit?id=${id}` });
    },
    reset() {
      this.formData = {
        sellType: "",
        typeId: "",
      };
    },
    onSearch() {
      this.queryParams = {
        ...this.queryParams,
        ...this.formData,
      };
      // console.info(this.queryParams)
      this.getListData({
        page: 1,
        limit: 10,
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.create {
  margin-bottom: 20px;
}
</style>
